package cn.ylhx.venue.backgroundClient.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import cn.ylhx.venue.background.dao.BackgroundDao;
import cn.ylhx.venue.background.entity.Background;
import cn.ylhx.venue.backgroundClient.dao.ClientDao;
import cn.ylhx.venue.backgroundClient.service.ClientService;
import cn.ylhx.venue.common.web.PageObject;
import cn.ylhx.venue.system.entity.User;

@Service
public class ClientServiceImpl implements ClientService{
	@Resource
	private ClientDao clientDao;
	@Resource BackgroundDao backgroundDao;
	@Override
	public Map<String, Object> findObjects(Background background, PageObject pageObject) {
		User user = (User) SecurityUtils.getSubject().getSession().getAttribute("currentUser");
		String manager=user.getUsername();
		Integer cid=clientDao.findIdByName(manager);
		String tableName="site_space_"+cid;
		List<Map<String, Object>> list =  backgroundDao.findObjects(tableName,background, pageObject);
		pageObject.setRowCount(backgroundDao.getRowCounts(tableName,background));	
		Map<String,Object> map=new HashMap<>();
		map.put("list", list);
		map.put("pageObj",pageObject);
		return map;
	}
	@Override
	public void changeState(Background background) {
		User user = (User) SecurityUtils.getSubject().getSession().getAttribute("currentUser");
		String manager=user.getUsername();
		Integer cid=clientDao.findIdByName(manager);
		String tableName="site_space_"+cid;
		if(background.getId()==null){
			throw new RuntimeException("修改用户状态，用户id不能为空！");
		}
		if(background.getValid()==null){
			throw new RuntimeException("修改用户状态，valid值不能为空！");
		}
		int i = backgroundDao.changeState(tableName,background);
		if(i!=1){
			throw new RuntimeException("切换用户启用禁用状态失败！");
		}
	}
	
	
	

}
